package com.umer.pinban.scan;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.recycleview.PullLoadMoreRecyclerView;
import com.common.ui.recycleview.listener.DataStateListener;
import com.common.ui.recycleview.listener.OptListener;
import com.common.ui.statelayout.StateLayout;
import com.common.utils.AntiShake;
import com.common.utils.Constants;
import com.common.utils.EventConstants;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.base.BasicActivity;
import com.umer.pinban.bean.customer.CommodityListBean;
import com.umer.pinban.scan.adapter.SelectJiaGouProjectAdapter;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class SelectJiaGouProjectActivity extends BasicActivity implements DataStateListener,
        PullLoadMoreRecyclerView.PullLoadMoreListener, OptListener {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.linear_add)
    LinearLayout linearAdd;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView pullLoadMoreRecyclerView;
    @BindView(R.id.state_layout)
    StateLayout stateLayout;
    private UserInfo userInfo;
    private String keywords = "";
    private String customerId = "";
    private List<CommodityListBean> commodityListBeanList = new ArrayList<>();
    private SelectJiaGouProjectAdapter selectJiaGouProjectAdapter;

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_jia_gou_select;
    }

    @OnClick({R.id.linear_back,R.id.linear_add})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                finish();
                break;
            case R.id.linear_add:
                List<CommodityListBean> infos = new ArrayList<>();
                for(int i=0;i<selectJiaGouProjectAdapter.getDataSource().size();i++){
                    if(selectJiaGouProjectAdapter.getDataSource().get(i).getCount()>0){
                        infos.add(selectJiaGouProjectAdapter.getDataSource().get(i));
                    }
                }
                if(infos.size()>0){
                    MsgBean msgBean = new MsgBean();
                    msgBean.setObject(infos);
                    msgBean.setFlag(EventConstants.jiagou_project);
                    getEventBus().post(msgBean);
                    finish();
                }else {
                    showToast("请选择加购项目");
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {
        int index = (int) object;
        switch (view.getId()) {
            case R.id.tv_jian_jiagou:
                int count = selectJiaGouProjectAdapter.getDataSource().get(index).getCount();
                if (count == 0) {
                    return;
                } else {
                    count = count - 1;
                    selectJiaGouProjectAdapter.getDataSource().get(index).setCount(count);
                }
                selectJiaGouProjectAdapter.notifyDataSetChanged();
                break;

            case R.id.tv_jia_jiagou:
                int count2 = selectJiaGouProjectAdapter.getDataSource().get(index).getCount();
                count2 = count2 + 1;
                selectJiaGouProjectAdapter.getDataSource().get(index).setCount(count2);
                selectJiaGouProjectAdapter.notifyDataSetChanged();
                break;
            default:break;
        }
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        customerId = super.getIntent().getStringExtra(customerId);
        initView();
    }

    @Override
    public void loadData() {
        if (selectJiaGouProjectAdapter.getPage() == 1) {
            stateLayout.showLoadingView();
        }
        selectCommodityList();
    }

    @Override
    public void onRefresh() {
        selectJiaGouProjectAdapter.reset();
        selectCommodityList();
    }

    @Override
    public void onLoadMore() {
        selectCommodityList();
    }

    @Override
    public void onDataState(int flag) {
        //1:没有网络 2:没有数据 3:获取数据失败 4:显示内容
        switch (flag) {
            case 1:
                stateLayout.showNoNetworkView();
                break;
            case 2:
                stateLayout.showEmptyView();
                break;
            case 3:
                stateLayout.showErrorView();
                break;
            case 4:
                stateLayout.showContentView();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.selectCommodityList:
                commodityListBeanList = (List<CommodityListBean>) t.getT();
                selectJiaGouProjectAdapter.addDataSource(commodityListBeanList, t);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.selectCommodityList:
                swipeRefreshLayout.setRefreshing(false);
                showToast(t.getDesc());
                break;
            default:
                break;
        }
    }

    private void selectCommodityList() {
        OkEntityListRequest<CommodityListBean> request = new OkEntityListRequest<>(R.id.selectCommodityList,
                Constants.selectCommodityList, CommodityListBean.class);
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("type", 0);
        request.addParams("customerId", customerId);
        request.addParams("page",selectJiaGouProjectAdapter.getPage());
        request.addParams("pageSize",10);
        request.addParams("keywords",keywords);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private void initView() {
        stateLayout.setRefreshListener(new StateLayout.OnViewRefreshListener() {
            @Override
            public void refreshClick() {
                loadData();
            }

            @Override
            public void loginClick() {
            }
        });
        pullLoadMoreRecyclerView.init(recyclerView, swipeRefreshLayout);
        selectJiaGouProjectAdapter = new SelectJiaGouProjectAdapter(this,
                R.layout.activity_select_jiagou_peoject_item, pullLoadMoreRecyclerView,
                this, this);
        pullLoadMoreRecyclerView.setAdapterTwo(selectJiaGouProjectAdapter);
        pullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorAccent, R.color.colorAccent);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法

       /* //单击
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView())
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                    }
                });
        //长按
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView()).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
                return false;
            }
        });*/
    }
}
